//获取职位信息
import {getIndicator} from "@/services/RadarChartService";

export function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = Math.random() * 16 | 0,
      v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

export function randomRgbColor() { //随机生成RGB颜色
  let r = Math.floor(Math.random() * 256); //随机生成256以内r值
  let g = Math.floor(Math.random() * 256); //随机生成256以内g值
  let b = Math.floor(Math.random() * 256); //随机生成256以内b值
  return `rgba(${r},${g},${b},.5)`; //返回rgb(r,g,b)格式颜色
}

function send(data) {
  if (data.method == null) {
    data.method = "GET";
  }
  if (data.async == null) {
    data.async = true;
  }
  if (data.contentType == null) {
    data.contentType = "application/json;charset=UTF-8";
  }
  let oAjax = new XMLHttpRequest();
  oAjax.open(data.method, data.url, data.async);

  //3,setHeader,get请求不需要
  oAjax.setRequestHeader("Content-type", data.contentType);
  let res = null;
  //4，定义返回触发的函数，定义在send之前，不然同步请求就出问题
  oAjax.onreadystatechange = function () {
    //6,通过状态确认完成
    if (oAjax.readyState == 4 && oAjax.status == 200) {
      res = oAjax.responseText;
      if (data.fn) {
        fn(res);
      }
    } else {
      throw Error("http state error " + oAjax.status + "[" + oAjax.statusText + "]");
    }
  };
  if (data.body) {
    oAjax.send(data.body);
  } else {
    oAjax.send();
  }
  if (data.async == false) {
    return res;
  }
}


//页面缓存
let allData = {indicatorMap: {}, abilityMap: {}, jobMap: {}};

export function listByIndicator(name) {
  let data = send({method: "GET", url: `/rest/v1/indicator/list`, async: false});
  let array = JSON.parse(data);
  let res = [];
  for (let item of array) {
    if (item.name.indexOf(name) != -1) {
      res.push({name: item.name, value: item.indicatorId});
    }
  }
  return res;
}


export function listByAbility(name) {
  let data = send({method: "GET", url: `/rest/v1/ability/list`, async: false});
  let array = JSON.parse(data);
  let res = [];
  for (let item of array) {
    if (item.name.indexOf(name) != -1) {
      res.push({name: item.name, value: item.abilityId});
    }
  }
  return res;
}

export function listByJob(name) {
  let data = send({method: "GET", url: `/rest/v1/job/list`, async: false});
  let array = JSON.parse(data);
  let res = [];
  for (let item of array) {
    if (item.name.indexOf(name) != -1) {
      res.push({name: item.name, value: item.jobId});
    }
  }
  return res;
}


export function getIndicatorById(id) {
  let res = allData.indicatorMap[id];
  if (!allData.indicatorMap[id]) {
    let data = send({method: "GET", url: `/rest/v1/indicator/id/${id}`, async: false});
    let s = JSON.parse(data);
    allData.indicatorMap[id] = s.data;
    res = s.data;
  }
  return res;
}

export function listScore(max) {
  if (!max) {
    max = 100;
  }
  let newVar = [{name: "A", value: 100}, {name: "B", value: 80}, {name: "C", value: 60}, {
    name: "D",
    value: 40
  }, {name: "E", value: 20}, {name: "F", value: 0}];
  let res = [];
  for (let item of newVar) {
    if (item.value <= max) {
      res.push(item);
    }
  }

  return res;
}

export function format2Letters(value) {
  let map = {100: "A", 80: "B", 60: "C", 40: "D", 20: "E", 0: "F"};
  return map[formatScore(value)];

}

export function formatScore(value) {
  let num = new Number(value) % 101;
  return "" + Math.floor(num / 20) * 20;
}

export function getAbilityById(id) {
  let res = allData.abilityMap[id];
  if (!allData.abilityMap[id]) {
    let data = send({method: "GET", url: `/rest/v1/ability/id/${id}`, async: false});
    let s = JSON.parse(data);
    allData.abilityMap[id] = s.data;
    res = s.data;
  }
  return res;
}

export function getJobById(id) {
  let res = allData.jobMap[id];
  if (!allData.jobMap[id]) {
    let data = send({method: "GET", url: `/rest/v1/job/id/${id}`, async: false});
    let s = JSON.parse(data);
    allData.jobMap[id] = s.data;
    res = s.data;
  }
  return res;
}
